#include once "fbgfx.bi"

dim shared as ushort PREMULDIV(16*4)

scope
  dim as integer SUM,DIVE
  for DIVE = 0 to 15
    for SUM = 0 to 3    
      PREMULDIV((DIVE shl 2)+SUM) = (128/(DIVE+1))
    next SUM
  next DIVE
end scope

sub ResamplePut(TGT as any ptr=0,CENX as short,CENY as short, SRC as any ptr,SIZX as short,SIZY as short,MYALPHA as integer=-256)
  
  static as any ptr IMGB,PTA,PTB,PTBB
  static as integer TX,TY,OBX,TDX
  static as integer BX,BY,USY,SSA,SSB
  static as integer RESSKPCNT,RESYSKP
  static as double DTMP
  
  ' ***** Check parameters to see if they are ok ********
  if SRC = 0 then exit sub    
  TX = cptr(fb.image ptr, SRC)->width
  TY = cptr(fb.image ptr, SRC)->height
  if TX<1 or TY<1 or SIZX<1 or SIZY<1 then exit sub  
  if TX > SIZX then BX=TX else BX=SIZX
  if TY > SIZY then BY=TY else BY=SIZY
  if (BX*BY) > OBX then
    BX *= 1.5: BY *= 1.5
    OBX = BX*BY
    if IMGB then Imagedestroy(IMGB)
    IMGB = ImageCreate(BX,BY,0)
    if (cast(uinteger,IMGB) mod 16) then
      asm        
        mov ESI,[IMGB]
        mov EDI,ESI
        and EDI,(not 15)
        add EDI,16
        mov [IMGB],EDI
        mov ecx, sizeof(fb.image)
        rep movsb
      end asm
    end if
  end if  
  if TGT = screenptr then TGT=0
  
  ' ***** pointer data / header ******
  PTA = (SRC+sizeof(fb.image)) 
  PTB = (IMGB+sizeof(fb.image))
  cptr(fb.image ptr, IMGB)->width = SIZX
  cptr(fb.image ptr, IMGB)->height = SIZY
  cptr(fb.image ptr, IMGB)->pitch = SIZX*4
  
  ' ***** check if special pointer is needed ****
  if SIZY >= TY then 
    PTBB=PTB+((SIZY-TY)*cptr(fb.image ptr, IMGB)->pitch) 
    asm
      mov ESI,[PTBB]
      mov EDI,ESI
      and EDI,(not 15)
      add EDI,16
      mov [PTBB],EDI
    end asm
  else 
    PTBB=PTB
  end if
  
  ' ==================================================================================
  ' ********************************* reduced width **********************************
  ' ==================================================================================
  if SIZX < TX then    
    DTMP=(TX/SIZX):RESSKPCNT=4
    while DTMP > 16
      DTMP /= 2
      RESSKPCNT shl= 1
    wend
    TDX = DTMP*65536
    
    asm
      ' ************************************************************
      ' ************************* HORIZONTAL ***********************
      ' ************************************************************
      mov esi,[PTA]                  ' Load ESI with the pointer of source
      mov edi,[PTBB]                 ' Load EDI with the pointer of the destiny
      mov eax,[TDX]                  ' EDX will temporary hold the "fixed point" number  
      movd MM1,[SIZX]                ' MM1 is receiving the WIDTH of the destiny image
      mov edx,[TY]                   ' MM2 is receiving the HEIGHT of the source image
      mov ebx,[RESSKPCNT]            ' Skip Pixels Value
      shl edx,16                     ' Adjusting for use high bits
      push ebp                       ' saving base pointer
      movd MM0,esp                   ' saving stack pointer in one MMX register
      mov ebp,eax                    ' EBP is receiving a fixed point number from EDX
      mov esp,-1                     ' ESP starts with -1
      pxor MM7,MM7                   ' Clear MM7 that will be used as zero
      '------------------------------------------------------------------------------
      _NEXTLINE_:            ' Here start the next line of the image
      movd eax,MM1                   ' Getting WIDTH from mmx register
      mov dx,ax                      ' Setting WIDTH in DX
      '------------------------------------------------------------------------------
      .align 16
      _NEXTGROUP_:           ' Here Start the next group from one average
      add esp,ebp                    ' Adding a new pixel group counter
      xor ecx,ecx                    ' Clearing Pixel Counter
      '------------------------------------------------------------------------------
      cmp esp,65536                  ' Verify For optimization (no division)
      jb _ISONEPIXELA_               ' Go optimize      
      cmp esp,65536*2                ' Verify For optimization (no division)
      jb _ISTWOPIXELA_               ' Go optimize
      pxor MM4,MM4                   ' Clear MM4 that will be used as SUM holder
      cmp esp,65536*4                ' Verify For optimization (no division)
      jb _ISFOURPIXELA_               ' Go optimize
      .align 16
      _NEXTPIXEL_:           ' Here it will get a new pixel
      mov eax, [esi]                 ' Read next Pixel
      cmp eax, 0xFFFF00FF            ' Is Transparent?
      jne SKPTT                      ' no? ignore
      mov eax,0x00808080             ' yes? gray with alpha 0
      SKPTT:                 ' Skip point for trans pixel
      inc ecx                        ' Increment Pixel Counter
      add esi,ebx                    ' point source to the next pixel
      movd MM5,eax                   ' Load Components in MMX register
      punpcklbw MM5,MM7              ' Convert from 4 bytes to 4 words
      paddw MM4,MM5                  ' Add those 4 bytes to the sum register      
      sub esp,65536                  ' subtract one pixel (fix pt) from calc holder
      jns _NEXTPIXEL_                ' go get next pixel until ESP becomes less than 0
      '------------------------------------------------------------------------------
      movq MM5,[PREMULDIV+ECX*8-8]   ' Get Fixed Point Pixel Divider
      psrlw MM4,3                    ' 5 bits only (0-31)
      pmullw MM4,MM5                 ' Divide Sum by pixel count (aka ECX)
      psrlw MM4,4                    ' From 4 Fixed to 4 short
      packuswb MM4,MM4               ' Convert from 4 shorts to 4 bytes
      movd [edi],MM4                 ' Save Average Pixel
      add edi,4                      ' point to the next buffer
      dec dx                         ' Decrease WIDTH counter
      jnz _NEXTGROUP_                ' Go get next pixel =D
      '------------------------------------------------------------------------------
      sub edx,65536                  ' Decrease line counter
      jnz _NEXTLINE_                 ' Go do the next line
      jmp _HORENDA_                  ' Finished
      '------------------------------------------------------------------------------
      _ISONEPIXELA_:          ' Here is the optimization point (no division)
      mov eax, [esi]                 ' Read next Pixel
      sub esp,65536                  ' subtract one pixel (fix pt) from calc holder
      cmp eax, 0xFFFF00FF            ' Is Transparent?
      jne SKPTTAOA                   ' no? ignore
      mov eax,0x00808080             ' yes? gray with alpha 0
      SKPTTAOA:               ' Skip point for trans pixel
      inc ecx                        ' Increment Pixel Counter
      add esi,ebx                    ' point source to the next pixel
      stosd                          ' Store pixel and increment
      dec dx                         ' Decrease WIDTH counter
      jnz _NEXTGROUP_                ' Go get next pixel =D
      sub edx,65536                  ' Decrease line counter
      jnz _NEXTLINE_                 ' Go do the next line
      jmp _HORENDA_                  ' Finished
      '------------------------------------------------------------------------------
      _ISTWOPIXELA_:         ' This is optimization (half)
      mov eax, [esi]                 ' Read next Pixel
      cmp eax, 0xFFFF00FF            ' Is Transparent?
      jne SKPTTOB                    ' no? ignore
      mov eax,0x00808080             ' yes? gray with alpha 0
      SKPTTOB:               ' Skip point for trans pixel
      and eax,0xFEFEFEFE             ' Preparing for shr 1
      add esi,ebx                    ' point source to the next pixel
      shr eax,1
      mov ecx, [esi]                 ' Read next Pixel
      cmp ecx, 0xFFFF00FF            ' Is Transparent?
      jne SKPTTOC                    ' no? ignore
      mov ecx,0x00808080             ' yes? gray with alpha 0
      SKPTTOC:               ' Skip point for trans pixel
      and ecx,0xFEFEFEFE             ' Preparing for shr 1
      add esi,ebx                    ' point source to the next pixel
      shr ecx,1
      sub esp,65536*2                ' subtract one pixel (fix pt) from calc holder
      add eax,ecx
      mov [edi],eax                 ' Save Average Pixel
      add edi,4                      ' point to the next buffer
      dec dx                         ' Decrease WIDTH counter
      jnz _NEXTGROUP_                ' Go get next pixel =D
      sub edx,65536                  ' Decrease line counter
      jnz _NEXTLINE_                 ' Go do the next line
      jmp _HORENDA_                  ' Finished
      '------------------------------------------------------------------------------
      _ISFOURPIXELA_:         ' This is optimization (half)
      mov eax, [esi]                 ' Read next Pixel
      cmp eax, 0xFFFF00FF            ' Is Transparent?
      jne SKPTTOF                    ' no? ignore
      mov eax,0x00808080             ' yes? gray with alpha 0
      SKPTTOF:               ' Skip point for trans pixel
      movd MM5,eax                   ' Load Components in MMX register
      punpcklbw MM5,MM7              ' Convert from 4 bytes to 4 words
      add esi,ebx                    ' point source to the next pixel
      paddw MM4,MM5                  ' Add those 4 bytes to the sum register
      mov eax, [esi]                 ' Read next Pixel
      cmp eax, 0xFFFF00FF            ' Is Transparent?
      jne SKPTTOG                    ' no? ignore
      mov eax,0x00808080             ' yes? gray with alpha 0
      SKPTTOG:               ' Skip point for trans pixel
      movd MM5,eax                   ' Load Components in MMX register
      punpcklbw MM5,MM7              ' Convert from 4 bytes to 4 words
      add esi,ebx                    ' point source to the next pixel
      paddw MM4,MM5                  ' Add those 4 bytes to the sum register
      mov eax, [esi]                 ' Read next Pixel
      cmp eax, 0xFFFF00FF            ' Is Transparent?
      jne SKPTTOD                    ' no? ignore
      mov eax,0x00808080             ' yes? gray with alpha 0
      SKPTTOD:               ' Skip point for trans pixel
      movd MM5,eax                   ' Load Components in MMX register
      punpcklbw MM5,MM7              ' Convert from 4 bytes to 4 words
      add esi,ebx                    ' point source to the next pixel
      paddw MM4,MM5                  ' Add those 4 bytes to the sum register
      mov eax, [esi]                 ' Read next Pixel
      cmp eax, 0xFFFF00FF            ' Is Transparent?
      jne SKPTTOE                    ' no? ignore
      mov eax,0x00808080             ' yes? gray with alpha 0
      SKPTTOE:               ' Skip point for trans pixel
      movd MM5,eax                   ' Load Components in MMX register
      punpcklbw MM5,MM7              ' Convert from 4 bytes to 4 words
      add esi,ebx                    ' point source to the next pixel
      paddw MM4,MM5                  ' Add those 4 bytes to the sum register
      sub esp,65536*4                ' subtract one pixel (fix pt) from calc holder
      psrlw MM4,2                    ' Divide Sum by 2
      packuswb MM4,MM4               ' Convert from 4 shorts to 4 bytes
      movd [edi],MM4                 ' Save Average Pixel
      add edi,4                      ' point to the next buffer
      dec dx                         ' Decrease WIDTH counter
      jnz _NEXTGROUP_                ' Go get next pixel =D
      sub edx,65536                  ' Decrease line counter
      jnz _NEXTLINE_                 ' Go do the next line
      '------------------------------------------------------------------------------
      _HORENDA_:             ' Finish of horizontal resize
      movd esp,MM0                   ' restoring stack pointer
      pop ebp                        ' restoin base pointer
      emms                           ' Clear MMX state
    end asm
  elseif SIZX >= TX then    
    
    ' ==================================================================================
    ' ******************************* increased width **********************************
    ' ==================================================================================
    
    DTMP=(TX/SIZX)
    TDX = DTMP*65536
    RESYSKP = cptr(fb.image ptr, SRC)->pitch
    
    ' ************************************************************
    ' ******************* HORIZONTAL WHEN BIGGER *****************
    ' ************************************************************
    asm      
      mov esi,[PTA]                  ' Load ESI with the pointer of source
      mov edi,[PTBB]                 ' Load EDI with the pointer of the destiny
      mov edx,[TDX]                  ' fixed point
      mov ecx,[TY]                   ' HEIGHT of the source
      mov ebx,[SIZX]                 ' WIDTH of the destiny
      mov eax,[RESYSKP]              ' PITCH of the source
      push ebp                       ' Saves Base Pointer
      mov ebp,edx                    ' fixed point
      movd MM0,esp                   ' Saves Stack Pointer
      mov esp,eax                    ' ESP = source PITCH
      shl ecx,16                     ' Use ECX high bits
      ' ----------------------------------------------------------
      _NEXTLINEC_:           ' Here start the next line
      mov cx,bx                      ' Restore Line Length
      xor edx,edx                    ' Clear Fixed Point Counter
      ' ----------------------------------------------------------
      _NEXTPIXELC_:          ' Here start next pixel
      mov eax,edx                    ' Get Counter
      shr eax,16                     ' Fixed Point to Short
      mov eax,[esi+eax*4]            ' Read Pixel
      cmp eax, 0xFFFF00FF            ' Is Transparent?
      jne HBSKPTTOC                  ' no? ignore
      mov eax,0x00808080             ' yes? gray with alpha 0
      HBSKPTTOC:             ' Skip point for trans pixel
      add edx,ebp                    ' increase fixed point counter
      stosd                          ' Save Pixel and point to next
      dec cx                         ' Decrease pixel counter
      jnz _NEXTPIXELC_               ' Go get next pixel (if any)
      ' ----------------------------------------------------------
      add esi,esp                    ' Adjust pointer for next line
      sub ecx,65536                  ' Decrease line counter (high bits)
      jnz _NEXTLINEC_                ' Go get next line (if any)
      ' ----------------------------------------------------------
      movd esp,MM0                   ' Restore Stack Pointer
      pop ebp                        ' Base Pointer from stack
      emms                           ' Clear MMX state
    end asm
    
    
  end if
  
  ' ==================================================================================
  ' ******************************* reduced height  **********************************
  ' ==================================================================================
  
  if SIZY < TY then
    
    DTMP=(TY/SIZY)
    RESSKPCNT=1
    while DTMP > 16
      DTMP /= 2
      RESSKPCNT shl= 1
    wend    
    TDX = DTMP*65536
    RESSKPCNT *= cptr(fb.image ptr, IMGB)->pitch
    RESYSKP = cptr(fb.image ptr, IMGB)->pitch
    
    asm
      ' ESI = Pointer of the source image
      ' EDI = Pointer of the destiny image
      ' EBP = Fixed Point Number of Pixels each group
      ' EAX = sum of byte intensity
      ' ECX = pixel counter (divisor)
      ' EBX = width/height of the destiny image
      ' ESP = controller of pixel/average
      ' EDX = temporary byte/dword converter & reminder
      
      ' ************************************************************
      ' ************************* VERTICAL *************************
      ' ************************************************************
      mov esi,[PTB]                  ' Load ESI with the pointer of source
      mov edi,[PTB]                  ' Load EDI with the pointer of the destiny
      mov ecx,[TDX]                  ' EDX will temporary hold the "fixed point" number
      movd MM1,[SIZY]                ' MM1 is receiving the HEIGHT of the destiny image
      movd MM2,[SSA]
      movd MM3,[SSB]
      mov edx,[SIZX]                 ' EDX is receiving the WIDTH of the destiny image
      mov eax,[RESYSKP]              ' destiny pitch
      mov ebx,[RESSKPCNT]            ' skip pixels(lines) pitch
      shl edx,16                     ' Using high bits
      push ebp                       ' saving base pointer
      movd MM0,esp                   ' saving stack pointer in one MMX register
      mov ebp,ecx                    ' EBP is receiving a fixed point number from EDX
      mov esp,-1                     ' ESP starts with -1
      pxor MM7,MM7                   ' Clear MMX register
      ' ----------------------------------------------------
      _NEXTLINEB_:           ' Here start the next line of the image
      mov ecx,edx
      movd edx,MM1                   ' Getting HEIGHT from mmx register
      or edx,ecx
      movd MM2,esi                   ' Saving Source Pointer
      movd MM3,edi                   ' Saving Destiny Pointer
      ' ----------------------------------------------------
      .align 16
      _NEXTGROUPB_:          ' Here Start the next group from one average
      add esp,ebp                    ' Adding a new pixel group counter
      cmp esp,65536                  ' Verify For optimization (no division)
      jb _VISONEPIXELA_              ' Go optimize
      cmp esp,65536*2                ' Verify For optimization (no division)
      jb _VISTWOPIXELA_              ' Go optimize
      pxor MM4,MM4                   ' Clear MM4 that will be used as SUM holder
      cmp esp,65536*4                ' Verify For optimization (no division)
      jb _VISFOURPIXELA_             ' Go optimize
      xor ecx,ecx                    ' Clearing ECX for be used as SUM holder
      ' ----------------------------------------------------
      .align 16
      _NEXTPIXELB_:          ' Here it will get a new pixel
      inc ecx                        ' Increment Pixel Counter
      movd MM5,[esi]                 ' Load Components in MMX register
      punpcklbw MM5,MM7              ' Convert from 4 bytes to 4 words
      add esi,ebx                    ' point source to the next pixel
      paddw MM4,MM5                  ' Add those 4 bytes to the sum register      
      sub esp,65536                  ' subtract one pixel (fix pt) from calc holder
      jns _NEXTPIXELB_               ' go get next pixel until ESP becomes less than 0
      '------------------------------------------------------------------------------
      movq MM5,[PREMULDIV+ECX*8-8]   ' Get Fixed Point Pixel Divider
      psrlw MM4,3                    ' 5 bits only (0-31)
      pmullw MM4,MM5                 ' Divide Sum by pixel count (aka ECX)
      psrlw MM4,4                    ' From 4 Fixed to 4 short
      packuswb MM4,MM4               ' Convert from 4 shorts to 4 bytes
      movd [edi],MM4                 ' Save Average Pixel
      add edi,eax                    ' point to the next buffer
      dec dx                         ' Decrease WIDTH counter
      jnz _NEXTGROUPB_               ' Go get next pixel =D
      '------------------------------------------------------------------------------
      movd esi,MM2                   ' Restoring Source Pointer
      movd edi,MM3                   ' Restoring Destiny Pointer
      add esi,4
      add edi,4
      sub edx,65536      
      jnz _NEXTLINEB_                ' Go do the next line
      jmp _ENDVERTA_
      '------------------------------------------------------------------------------
      _VISONEPIXELA_:
      mov ecx,[esi]
      mov [edi],ecx      
      add esi,ebx                    ' point source to the next pixel
      add edi,eax                    ' point to the next buffer
      sub esp,65536                  ' subtract one pixel (fix pt) from calc holder
      dec dx                         ' Decrease WIDTH counter
      jnz _NEXTGROUPB_               ' Go get next pixel =D
      movd esi,MM2                   ' Restoring Source Pointer
      movd edi,MM3                   ' Restoring Destiny Pointer
      add esi,4
      add edi,4
      sub edx,65536      
      jnz _NEXTLINEB_                ' Go do the next line
      jmp _ENDVERTA_
      '------------------------------------------------------------------------------
      _VISTWOPIXELA_:
      movd MM4,edx
      mov ecx, [esi]                 ' Read next Pixel      
      and ecx,0xFEFEFEFE             ' Preparing for shr 1
      add esi,ebx                    ' point source to the next pixel
      shr ecx,1      
      mov edx, [esi]                 ' Read next Pixel      
      and edx,0xFEFEFEFE             ' Preparing for shr 1
      add esi,ebx                    ' point source to the next pixel
      shr edx,1      
      add edx,ecx
      mov [edi],edx
      add edi,eax                    ' point to the next buffer
      sub esp,65536*2                ' subtract one pixel (fix pt) from calc holder
      movd edx,MM4
      dec dx                         ' Decrease WIDTH counter
      jnz _NEXTGROUPB_               ' Go get next pixel =D
      movd esi,MM2                   ' Restoring Source Pointer
      movd edi,MM3                   ' Restoring Destiny Pointer
      add esi,4
      add edi,4
      sub edx,65536      
      jnz _NEXTLINEB_                ' Go do the next line
      jmp _ENDVERTA_
      '----------------------------------------------------------------------------      
      _VISFOURPIXELA_:
      movd MM5,[esi]                 ' Load Components in MMX register      
      punpcklbw MM5,MM7              ' Convert from 4 bytes to 4 words
      add esi,ebx                    ' point source to the next pixel
      paddw MM4,MM5                  ' Add those 4 bytes to the sum register
      movd MM5,[esi]                 ' Load Components in MMX register
      punpcklbw MM5,MM7              ' Convert from 4 bytes to 4 words
      add esi,ebx                    ' point source to the next pixel
      paddw MM4,MM5                  ' Add those 4 bytes to the sum register
      movd MM5,[esi]                 ' Load Components in MMX register      
      punpcklbw MM5,MM7              ' Convert from 4 bytes to 4 words
      add esi,ebx                    ' point source to the next pixel
      paddw MM4,MM5                  ' Add those 4 bytes to the sum register
      movd MM5,[esi]                 ' Load Components in MMX register
      punpcklbw MM5,MM7              ' Convert from 4 bytes to 4 words
      add esi,ebx                    ' point source to the next pixel
      paddw MM4,MM5                  ' Add those 4 bytes to the sum register
      psrlw MM4,2                    ' Divide Sum by 2
      packuswb MM4,MM4               ' Convert from 4 shorts to 4 bytes
      movd [edi],MM4      
      add edi,eax                    ' point to the next buffer
      sub esp,65536*4                ' subtract one pixel (fix pt) from calc holder
      dec dx                         ' Decrease WIDTH counter
      jnz _NEXTGROUPB_               ' Go get next pixel =D
      movd esi,MM2                   ' Restoring Source Pointer
      movd edi,MM3                   ' Restoring Destiny Pointer
      add esi,4
      add edi,4
      sub edx,65536      
      jnz _NEXTLINEB_                ' Go do the next line
      _ENDVERTA_:
      movd esp,MM0                   ' restoring stack pointer
      pop ebp                        ' restoin base pointer
      emms
    end asm  
    
  elseif SIZY >= TY then 
    
    ' ==================================================================================
    ' ******************************* increased height **********************************
    ' ==================================================================================
    DTMP=(TY/SIZY)
    TDX = DTMP*65536
    RESYSKP = cptr(fb.image ptr, IMGB)->pitch
    
    ' ************************************************************
    ' ******************** VERTICAL WHEN BIGGER ******************
    ' ************************************************************
    asm
      mov esi,[PTBB]                ' Load ESI with the pointer of source
      mov edi,[PTB]                 ' Load EDI with the pointer of the destiny
      mov edx,[TDX]                 ' fixed point
      mov ebx,[TY]                  ' WIDTH of the destiny
      mov eax,[RESYSKP]             ' PITCH of the source
      push ebp                      ' saving base pointer
      mov ebp,edx                   ' fixed point
      movd MM0,esp                  ' saving stack pointer
      mov esp,eax                   ' ESP = source/destiny pitch
      mov edx,-1                    ' fixed point counter start with -1
      ' ----------------------------------------------------------
      _VNEXTGROUPC_:         ' here start the next line from source
      add edx,65536                 ' one line in fixed point
      ' ---------------------------------------------------------
      _VNEXTLINEC_:          ' here start the next line into destiny
      mov ecx,esp                   ' get pitch
      shr ecx,2                     ' transform into pixels
      rep movsd                     ' copy a line
      sub esi,esp                   ' point to the start of the line again
      sub edx,ebp                   ' decrease decimal fixed point
      jns _VNEXTLINEC_              ' go draw next line
      ' ---------------------------------------------------------
      add esi,esp                   ' point to the next source line
      dec bx                        ' decrease source lines count
      jnz _VNEXTGROUPC_             ' got draw next group of lines
      ' ----------------------------------------------------------
      movd esp,MM0                  ' restoring stack pointer
      pop ebp                       ' restore base pointer from stack
      emms                          ' clear MMX status
    end asm
    
  end if
  
  if MYALPHA <> -256 then
    ' *** plotting result in target (alpha) ***
    TDX = cint(SIZX)*SIZY
    asm      
      mov esi,[PTB]      
      mov ecx,[TDX]
      _TRANSNEXTPIXEL_:
      cmp byte ptr [esi+3], 8
      ja _IGTRANS_
      mov dword ptr [esi],0xFFFF00FF
      _IGTRANS_:
      add esi,4
      dec ecx
      jnz _TRANSNEXTPIXEL_
    end asm
    put TGT,(CENX-(SIZX shr 1),CENY-(SIZY shr 1)),IMGB,alpha,MYALPHA
  else        
    ' *** plotting result in target ***
    put TGT,(CENX-(SIZX shr 1),CENY-(SIZY shr 1)),IMGB,alpha
  end if
  
end sub

